import warnings
from functools import wraps
from sqlalchemy.exc import IntegrityError


def close_db_connection(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        result = func(*args, **kwargs)
        args[0].session.close()
        return result
    return wrapper


def update_db_operation(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        session = args[0].session
        try:
            with warnings.catch_warnings():
                warnings.simplefilter("ignore")
                func(*args, **kwargs)
                session.commit()
                session.close()
        except IntegrityError:
            session.rollback()
            session.close()
    return wrapper